﻿using Gp.Scripts.Core;
using UnityEngine.EventSystems;

namespace Gp.Scripts.GUI
{
    public class BoxSkillButton : BaseSkillButton, IPointerEnterHandler, IPointerExitHandler
    {
        public SkillEntity SkillEntity;
        public SkillButtonInfo RenderInfo => SkillEntity?.RenderInfo() ?? default;
        public SkillToolTipsController ToolTipsController { get; private set; }

        public override void Init(SkillEntity item, SkillToolTipsController controller)
        {
            SkillEntity = item;
            ToolTipsController = controller;

            button.onClick.AddListener(
                () =>
                {
                    if (SkillEntity.CanSelect().Result())
                    {
                        Global.SendEvent(new Event_SkillSelect(SkillEntity));
                    }
                }
            );


            var info = SkillEntity.RenderInfo();
            icon.sprite = info.Icon;
            gameObject.SetActive(true);
        }


        private void LateUpdate()
        {
            if (SkillEntity == null) return;
            // todo: 根据更新事件更新
            button.interactable = SkillEntity.CanSelect().Result();
        }


        public void OnPointerEnter(PointerEventData eventData)
        {
            if (ToolTipsController)
            {
                ToolTipsController.Display(this);
            }
        }


        public void OnPointerExit(PointerEventData eventData)
        {
            if (ToolTipsController)
            {
                ToolTipsController.Hide();
            }
        }
    }
}